<?php
/**
 * @copyright: ©2024 永州 微聚 商 网络科技有限公司
 * @link: https://www.weiJuyunke.com
 */
namespace app\api\controller\consume;
use app\admin\model\ActivityConsumer;
use app\admin\model\CouponConsumer;
use app\admin\model\CouponOrder;
use app\common\controller\AdminController;
use think\App;

/**
 * @ControllerAnnotation(title="会员管理")
 */
class Coupon extends AdminController
{

    use \app\admin\traits\Curd;
    protected $sort = [
        'create_time' => 'desc'
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\Coupon();
    }


    public function index()
    {
        $request = $this->request;
        $param = $request->param();
        $page = input("page");
        $limit = input("limit");
        $where[] = ['coupon.is_delete','=',0];

        if ($this->request->platform_id >0 ){
            $where[] = ['platform_id','=',$this->request->platform_id];
        }
        if (isset( $param['name']) &&   $param['name']){
            $where[] = ['coupon.name','like',"%{$param['name']}%"];
        }

        if (isset( $param['telphone']) &&   $param['telphone'] ){
            $where[] = ['platformInfo.telphone','=',$param['telphone']];
        }
        $count = $this->model->withJoin('platformInfo', 'LEFT')
            ->where($where)
            ->count();
        $list = $this->model->withJoin('platformInfo', 'LEFT')
            ->where($where)
            ->page($page, $limit)
            ->order($this->sort)
            ->select();

        foreach ($list as $key => $value){
            $list[$key]['start_time']  = date('Y-m-d H:i',$value['start_time']);
            $list[$key]['end_time']  = date('Y-m-d H:i',$value['end_time']);
            $list[$key]['desc']  = strip_tags($value['desc']);
            $couponConsumerModel = new CouponConsumer();
            $couponConsumerList   = $couponConsumerModel->where(['coupon_id'=> $value['id'],'is_delete' => 0])->select()->toArray();
            $list[$key]['consumeids'] = array_column($couponConsumerList,'consumer_id');

        }

        return msg([
            'list' => $list,
            'count' => $count
        ], 0, "success");
    }

    public function add()
    {

        $request = $this->request;
        $param = $request->param();
        $data = [
            'platform_id' =>$this->request->platform_id,
            'name' => $param['name'],
            'price' => $param['price'],
            'desc' => htmlspecialchars_decode($param['desc']),
            'start_time' => strtotime($param['start_time']),
            'end_time' => strtotime($param['end_time']) + 86399,
            'coupon_img' => $param['coupon_img'],
            'inventory' => $param['inventory'],
            'sales' => $param['sales'],
            'sponsor_name' => $param['sponsor_name'],
            'sponsor_telphone' => $param['sponsor_telphone'],
        ];
        if (empty($param['name'])){
            return  msg([], 1, "请输入卡券名称") ;
        }
        if (empty($param['coupon_img'])){
            return  msg([], 1, "请上传图片") ;
        }
        if($param['latitude'] != '39.89945'){
            $data['latitude']  = $param['latitude'];
        }
        if($param['longitude'] != '116.40969'){
            $data['longitude']  = $param['longitude'];
        }

//        if (empty($param['consumeids'])){
//            return  msg([], 1, "请选择优惠券") ;
//        }
        $this->model->save($data);
        return  msg([], 0, "保存成功") ;
    }

    public function edit(){
        $request = $this->request;
        $param = $request->param();
        $CouponOrderModel = new CouponOrder();
        $couponOrder= $CouponOrderModel->where(['coupon_id'=>  $param['id']])->find();
        if ($couponOrder){
            return  msg([], 1, "此卡券已有用户购买，请勿修改") ;
        }
        $data = [
            'platform_id' =>$this->request->platform_id,
            'name' => $param['name'],
            'price' => $param['price'],
            'desc' => htmlspecialchars_decode($param['desc']),
            'start_time' => strtotime($param['start_time']),
            'end_time' => strtotime($param['end_time']) + 86399,
            'sponsor_name' => $param['sponsor_name'],
            'sponsor_telphone' => $param['sponsor_telphone'],
        ];
        if($param['latitude'] != '39.89945'){
            $data['latitude']  = $param['latitude'];
        }
        if($param['longitude'] != '116.40969'){
            $data['longitude']  = $param['longitude'];
        }
        $this->model->where(['id' => $param['id']])->update($data);
        return  msg([], 0, "保存成功") ;
    }

    public function couponConsumer()
    {
        $request = $this->request;
        $param = $request->param();
        $page = input("page");
        $limit = input("limit");
        $where[] = ['coupon_consumer.is_delete', '=', 0];


        if ($this->request->platform_id > 0) {
            $where[] = ['platform_id', '=', $this->request->platform_id];
        }
        if (isset($param['name']) && $param['name']) {
            $where[] = ['couponInfo.name', 'like', "%{$param['coupon_name']}%"];
        }


        if (isset( $param['store']) &&   $param['store'] ){
            $where[] = ['storeInfo.telphone|storeInfo.store_name','like',"%".$param['store']."%"];
        }
        $couponConsumerModel = new CouponConsumer();
        $count = $couponConsumerModel
            ->withJoin(['platformInfo','couponInfo','consumerInfo','storeInfo'], 'LEFT')
            ->where($where)
            ->count();
        $list = $couponConsumerModel
            ->withJoin(['platformInfo','couponInfo','consumerInfo','storeInfo'], 'LEFT')
            ->where($where)
            ->page($page, $limit)
            ->order($this->sort)
            ->select();
        return msg([
            'list' => $list,
            'count' => $count
        ], 0, "success");
    }

    public function removecouponConsumer(){
        $request = $this->request;
        $param = $request->param();
        $CouponOrderModel = new CouponOrder();
        $couponConsumerModel = new CouponConsumer();
        $couponConsumerInfo = $couponConsumerModel->where(['id' =>$param['id'] ])->find();
        $couponOrder= $CouponOrderModel->where(['coupon_id'=>  $couponConsumerInfo['coupon_id']])->find();
        if ($couponOrder){
            return  msg([], 1, "此卡券已有用户购买，请勿修改") ;
        }
        $couponConsumerModel->whereIn('id', $param['id'])->update(['is_delete' => 1]);

        return msg([], 0, "删除成功");
    }

    public function orderList(){
        $request = $this->request;
        $param = $request->param();
        $page = input("page");
        $limit = input("limit");
        $where = [];

        if ($this->request->platform_id >0 ){
            $where[] = ['coupon_order.platform_id','=',$this->request->platform_id];
        }
        $couponOrder = new CouponOrder();
        if (isset( $param['platform_name']) &&   $param['platform_name'] ){
            $where[] = ['platformInfo.name','=',$param['platform_name']];
        }
        if (isset($param['name']) && $param['name']) {
            $where[] = ['couponInfo.name', 'like', "%{$param['name']}%"];
        }
        $count = $couponOrder->withJoin([
            'platformInfo',
            'couponInfo',
             'userInfo'
        ], 'LEFT')
            ->where($where)
            ->count();
        $list = $couponOrder->withJoin([
            'platformInfo',
            'couponInfo',
            'userInfo'
        ], 'LEFT')
            ->where($where)
            ->page($page, $limit)
            ->order($this->sort)
            ->select();

        return msg([
            'list' => $list,
            'count' => $count
        ], 0, "success");
    }

    public function delete(){


        $request = $this->request;
        $param = $request->param();
        $this->model->whereIn('id', $param['id'])->update(['is_delete' => 1]);
        $couponConsumerModel = new CouponConsumer();
        $couponConsumerModel->whereIn('coupon_id', $param['id'])->update(['is_delete' => 1]);
        return msg([

        ], 0, "删除成功");
    }

    public function couponConsumerModify(){
        $request = $this->request;
        $param = $request->param();
        $couponConsumerModel = new CouponConsumer();
        $couponConsumerModel->where(['id' => $param['id']])->update([$param['field'] => $param['value'] ]);
        if ($param['value'] == 2){
            return msg([

            ], 0, "审核通过");
        }

        if ($param['value'] == 3){
            return msg([
                ], 0, "驳回成功");
        }

    }

}